import requests
import re
from datetime import datetime, timedelta

def convert_date(date_str):
    """转换日期字符串为标准格式"""
    now = datetime.now()
    if "小时前" in date_str:
        hours_ago = int(re.search(r"(\d+)小时前", date_str).group(1))
        date = now - timedelta(hours=hours_ago)
    elif "分钟前" in date_str:
        minutes_ago = int(re.search(r"(\d+)分钟前", date_str).group(1))
        date = now - timedelta(minutes=minutes_ago)
    elif "天前" in date_str:
        days_ago = int(re.search(r"(\d+)天前", date_str).group(1))
        date = now - timedelta(days=days_ago)
    elif "昨天" in date_str:
        date = now - timedelta(days=1)
        time_str = re.search(r"昨天(\d{1,2}:\d{2})", date_str).group(1)
        date = datetime.strptime(f"{date.strftime('%Y-%m-%d')} {time_str}", "%Y-%m-%d %H:%M")
    elif "前天" in date_str:
        date = now - timedelta(days=2)
        time_str = re.search(r"前天(\d{1,2}:\d{2})", date_str).group(1)
        date = datetime.strptime(f"{date.strftime('%Y-%m-%d')} {time_str}", "%Y-%m-%d %H:%M")
    elif "月" in date_str:
        date = datetime.strptime(f"{now.year}-{date_str}", "%Y-%m月%d日")
    else:
        date = now  # 如果没有匹配到任何规则，使用当前时间
    return date.strftime("%Y/%m/%d %H:%M")



def baidu(company):
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
    url = f'http://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd={company}'
    res = requests.get(url, headers=headers).text

    p_href = '<h3 class="news-title_1YtI1 "><a href="(.*?)"'
    href = re.findall(p_href, res, re.S)
    p_title = '<h3 class="news-title_1YtI1 ">.*?>(.*?)</a>'
    title = re.findall(p_title, res, re.S)
    p_date = '<span class="c-color-gray2 c-font-normal c-gap-right-xsmall" .*?>(.*?)</span>'
    date = re.findall(p_date, res)
    p_source = '<span class="c-color-gray" .*?>(.*?)</span>'
    source = re.findall(p_source, res)
    p_abstract = '<span class="c-font-normal c-color-text" .*?>(.*?)</span>'
    abstract = re.findall(p_abstract, res)
    min_len = min(len(title), len(href), len(date), len(source))

    #先不进行数清清洗
   
    file1 = open('数据挖掘报告.txt', 'a', encoding='utf-8')  # 注意缩进的细节，不要写到上面数据清洗的for循环里去
    file1.write(company + ' 数据挖掘完成!' + '\n\n')

    for i in range(min_len):  #len(title)可能导致输出错误的情况
        formatted_date = convert_date(date[i])#日期格式转换
        file1.write(str(i + 1) + '.' + title[i] + '(' + formatted_date + ' - ' + source[i] + ')' + '\n')
        file1.write(href[i] + '\n')  # "\n"表示换行
        file1.write('摘要：' + abstract[i] + '\n\n') 
    
    file1.write('-------------------'+'\n\n')
    file1.close()



companys = ['小米集团', '腾讯', '字节跳动', '可可豆动画' ] 
for i in companys:
    baidu(i)
    print(i + '百度新闻爬取成功'+ '\n'+'——————————————————————————————'+ '\n')

print('数据获取及生成报告成功')
